﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmallERP.MODEL.FoundationModel;
using SmallERP.DAL;
using Smallerp.Contracts;
using Smallerp.Contracts.DataContracts;
using System.Data;

namespace SmallERP.Bll.PinBan
{
    /// <summary>
    /// 大板的模板逻辑类
    /// </summary>
    public class BoardTemplateBLL
    {
        public BoardTemplateBLL(IOperateDB inOperateDb)
        {
            _OperateDb = inOperateDb;
        }
        IOperateDB _OperateDb;
        public List<BoardTemplate_DCT> GetAllTemplate(PrinterDCT printerInfo)
        {
            List<BoardTemplate_DCT> listResult = new List<BoardTemplate_DCT>();
            //using (smallerpDataContext db = new smallerpDataContext())
            //{

            //    foreach (var b in db.BoardTemplate.Where(bt => bt.printer.PrinterID == printerInfo.PrinterID).ToList())
            //    {

            //        if (!b.PaperFaceReference.IsLoaded)
            //        {
            //            b.PaperFaceReference.Load();
            //        }
            //        if (!b.PaperTypeReference.IsLoaded)
            //        {
            //            b.PaperTypeReference.Load();
            //        }
            //        listResult.Add(new BoardTemplate_Info()
            //        {
            //            BarCodeStatus = (SmallERP.COM.Status.PhotoBarCodeStatus)b.BarCodeState,
            //            Height = (float)b.Height,
            //            Length = (float)b.Length,
            //            PaperFaceId = b.PaperFace.PaperFaceId,
            //            PaperFaceName = b.PaperFace.PaperFaceName,
            //            PaperTypeId = b.PaperType.PaperTypeId,
            //            PaperTypeName = b.PaperType.PaperTypeName,
            //            PrinterId = printerInfo.PrinterID,
            //            TemplateId = b.ID,
            //            TemplateName = b.TemplateName
            //        });
            //    }
            //}
            string QueryBoardTemp = "select BarCodeState,Height,Length,BoardTemplate.PaperFaceId as PaperFaceId,paperface.PaperFaceName as PaperFaceName,papertype.PaperTypeId as PaperTypeId, papertype.PaperTypeName as PaperTypeName,id,TemplateName,MinPhotoSize FROM BoardTemplate,PaperFace,PaperType where BoardTemplate.PaperFaceId=PaperFace.PaperFaceId and BoardTemplate.PaperTypeId=PaperType.PaperTypeId and PrinterID=" + printerInfo.PrinterID;
            DataTable table = _OperateDb.QueryDataTable(QueryBoardTemp);
            for (int i = 0; i < table.Rows.Count; i++)
            {
                BoardTemplate_DCT dct = new BoardTemplate_DCT() { BarCodeStatus = (SmallERP.COM.Status.PhotoBarCodeStatus)table.Rows[i]["BarCodeState"],
                                                                  Height = (float)Convert.ToDecimal(table.Rows[i]["Height"]),
                                                                   Length=(float)Convert.ToDecimal(table.Rows[i]["Length"]),
                                                                  PaperFaceId = Convert.ToInt16(table.Rows[i]["PaperFaceId"]),
                                                                    PaperFaceName=Convert.ToString(table.Rows[i]["PaperFaceName"]), 
                                                                   PaperTypeId=Convert.ToInt16(table.Rows[i]["PaperTypeId"]),
                                                                   PaperTypeName=Convert.ToString(table.Rows[i]["PaperTypeName"]),
                                                                   PrinterId= printerInfo.PrinterID,
                                                                  TemplateId = Convert.ToInt16(table.Rows[i]["id"]),
                                                                  TemplateName = Convert.ToString(table.Rows[i]["TemplateName"]),
                                                                  MinPhotoSize = Convert.ToDecimal(table.Rows[i]["MinPhotoSize"])
                };
                listResult.Add(dct);
 
            }
                return listResult;
        }

        public int AddNewTemplate(BoardTemplate_DCT newT)
        {
            int result = 0;
            //if (newT != null)
            //{
            //    using (smallerpDataContext db = new smallerpDataContext())
            //    {

            //        db.AddToBoardTemplate(new BoardTemplate()
            //        {
            //            BarCodeState = (int)newT.BarCodeStatus,
            //            Height = newT.Height,
            //            Length = newT.Length,
            //            PaperFace = db.PaperFace.FirstOrDefault(pf => pf.PaperFaceId == newT.PaperFaceId),
            //            PaperType = db.PaperType.FirstOrDefault(pt => pt.PaperTypeId == newT.PaperTypeId),
            //            printer = db.printer.FirstOrDefault(p => p.PrinterID == newT.PrinterId),
            //            TemplateName = newT.TemplateName
            //        });
            //        try
            //        {
            //            db.SaveChanges();
            //        }
            //        catch
            //        {
            //            result = -2;
            //        }
            //    }
            //}
            //else
            //{
            //    result = -1;
            //}
            string insertBoardTemp = "insert into BoardTemplate(BarCodeState,Height,Length,PaperFaceId,PaperTypeId,PrinterId,TemplateName,MinPhotoSize) "
                                  + " values (" + (int)newT.BarCodeStatus + "," + newT.Height + "," + newT.Length + "," + newT.PaperFaceId + "," + newT.PaperTypeId + "," + newT.PrinterId + ",'" + newT.TemplateName + "'," + newT.MinPhotoSize + ")";
            result = _OperateDb.ExecuteNonQuery(insertBoardTemp);
            
            return result;
        }

        public  int DelTempalte(int p)
        {
            int result = 0;
            //if (p > 0)
            //{
            //    using (smallerpDataContext db = new smallerpDataContext())
            //    {
            //        db.DeleteObject(db.BoardTemplate.FirstOrDefault(t => t.ID == p));
            //        try
            //        {
            //            db.SaveChanges();
            //        }
            //        catch
            //        {
            //            result = -2;
            //        }
            //    }
            //}
            //else
            //{
            //    result = -1;
            //}
            result = _OperateDb.ExecuteNonQuery("delete from BoardTemplate where id=" + p);
            return result;
        }

        public int ModifyTemplate(BoardTemplate_DCT curT)
        {
            int result = 0;
            //if (curT == null)
            //{
            //    return -2;
            //}

            //using (smallerpDataContext db = new smallerpDataContext())
            //{
            //    BoardTemplate bt = db.BoardTemplate.FirstOrDefault(t => t.ID == curT.TemplateId);
            //    if (bt!=null)
            //    {
            //        bt.Height = curT.Height;
            //        bt.Length = curT.Length;
            //        bt.TemplateName = curT.TemplateName;
            //        bt.PaperFace = db.PaperFace.FirstOrDefault(pf => pf.PaperFaceId == curT.PaperFaceId);
            //        bt.PaperType = db.PaperType.FirstOrDefault(pt => pt.PaperTypeId == curT.PaperTypeId);
            //        bt.BarCodeState = (int)curT.BarCodeStatus;

            //        try
            //        {
            //            db.SaveChanges();
            //        }
            //        catch
            //        {
            //            result = -2;
            //        }
            //    }
            //    else
            //    {
            //        result = -1;
            //    }
            //}
            string UpdateBoardTemp = "update BoardTemplate set Height=" + curT.Height + ",Length=" + curT.Length + ",TemplateName='" + curT.TemplateName + "',PaperFaceId=" + curT.PaperFaceId + ", PaperTypeId=" + curT.PaperTypeId + ", BarCodeState=" + (int)curT.BarCodeStatus + ",MinPhotoSize=" + curT.MinPhotoSize + " where id=" + curT.TemplateId;
            result = _OperateDb.ExecuteNonQuery(UpdateBoardTemp);
            return result;
        }
    }
}
